using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.SqlClient;
using System.Data.Common;
using System.Diagnostics;
using System.Net.Mail;

public partial class DBAContactDetail : System.Web.UI.Page
{
    Microsoft.Practices.EnterpriseLibrary.Data.Database db = DatabaseFactory.CreateDatabase("Application_Data");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.btnDelete.Attributes.Add("OnCLick", "return confirm('Are you sure you want to delete this database from the repository?');");

            BindData();
        }
    }
    private void BindData()
    {
        DbCommand dbCommand1 = db.GetSqlStringCommand("SELECT * FROM [DBAContact] Where DBAContactID ='" + Request.QueryString["DBAContactID"].ToString() + "'");
        DataSet ds1 = db.ExecuteDataSet(dbCommand1);
        DataRow dr1 = ds1.Tables[0].Rows[0];

        this.txtDBAContactName.Text = dr1["DBAContactName"].ToString();
        this.txtDBAOfficePhone.Text = dr1["DBAOfficePhone"].ToString();
        this.txtDBAMobilePhone.Text = dr1["DBAMobilePhone"].ToString();
        this.txtDBAHomePhone.Text = dr1["DBAHomePhone"].ToString();
        this.txtDBAContactEmail.Text = dr1["DBAContactEMail"].ToString();
        this.txtDBAOnCallContactEmail.Text = dr1["DBAOnCallContactEMail"].ToString();
        this.chkboxDBANotificationFlag.Checked = Convert.ToBoolean(dr1["DBANotificationFlag"].ToString());
        this.chkboxDBAOnCallFlag.Checked = Convert.ToBoolean(dr1["DBAOnCallFlag"].ToString());
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE DBAContact SET DBAContactName = '" + this.txtDBAContactName.Text + "'" +
                                           ",  DBAOfficePhone = '" + this.txtDBAOfficePhone.Text + "'" +
                                           ",  DBAMobilePhone = '" + this.txtDBAMobilePhone.Text + "'" +
                                           ",  DBAHomePhone = '" + this.txtDBAHomePhone.Text + "'" +
                                           ",  DBAContactEMail = '" + this.txtDBAContactEmail.Text + "'" +
                                           ",  DBAOnCallContactEMail = '" + this.txtDBAOnCallContactEmail.Text + "'" +
                                           ",  DBANotificationFlag = '" + Convert.ToBoolean(this.chkboxDBANotificationFlag.Checked) + "'" +
                                           ",  DBAOnCallFlag = '" + Convert.ToBoolean(this.chkboxDBAOnCallFlag.Checked )+ "'" +
                                           " WHERE DBAContactID = '" + Request.QueryString["DBAContactID"].ToString() + "'");

        db.ExecuteNonQuery(dbCommand);

        Response.Redirect("~/siteadministration.aspx");
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("DELETE FROM DBAContact WHERE DBAContactID = '" + Request.QueryString["DBAContactID"].ToString() + "'");

        db.ExecuteNonQuery(dbCommand);

        Response.Redirect("~/siteadministration.aspx");
    }
    protected void btnTestNotificationEMail_Click(object sender, EventArgs e)
    {
        MailMessage msg = new MailMessage();

        msg.To.Add(this.txtDBAContactEmail.Text.ToString());
        msg.Subject = "DBA Alert: TEST MESSAGE";
        msg.Body = "--------------------------------------------------------- " + " \r\n" +
                   "DBA Alert      : TEST ALERT MESSAGE" + " \r\n" +
                   "--------------------------------------------------------- " + " \r\n" +
                   "TEST MESSAGE" + " \r\n" +
                   "--------------------------------------------------------- " + " \r\n" +
                   "END OF ALERT";

        SmtpClient client = new SmtpClient();
        client.Send(msg);

        MessageBox.Show("TEST ALERT MESSAGE SENT SUCCESSFULLY");
                

    }
    protected void btnTestAlertEM_Click(object sender, EventArgs e)
    {
        MailMessage msg = new MailMessage();

        msg.To.Add(this.txtDBAOnCallContactEmail.Text.ToString());
        msg.Subject = "DBA Alert: TEST MESSAGE";
        msg.Body = "--------------------------------------------------------- " + " \r\n" +
                   "DBA Alert      : TEST NOTIFICATION MESSAGE" + " \r\n" +
                   "--------------------------------------------------------- " + " \r\n" +
                   "TEST MESSAGE" + " \r\n" +
                   "--------------------------------------------------------- " + " \r\n" +
                   "END OF ALERT";

        SmtpClient client = new SmtpClient();
        client.Send(msg);

        MessageBox.Show("TEST NOTIFICATION MESSAGE SENT SUCCESSFULLY");
    }
}
